//
//  OAAddressViewController.m
//  OASystem
//
//  Created by zhoupengfei on 16/1/17.
//  Copyright © 2016年 zpf. All rights reserved.
//
#import "PersonViewController.h"
#import "OAAddressViewController.h"
#import "HttpTools.h"
#import "OADepAddressController.h"
#import "MBProgressHUD.h"
@interface AddressCellList : UITableViewCell

@property (weak, nonatomic) IBOutlet UIImageView *imageVIew;
@property (weak, nonatomic) IBOutlet UILabel *subject;


@end

@implementation AddressCellList



@end

@interface OAAddressViewController ()<UIAlertViewDelegate>
@property(nonatomic,strong)NSMutableArray* contents;
@property(strong,nonatomic)NSMutableArray * searchResult;
@end

@implementation OAAddressViewController

-(NSMutableArray*)contents{
    if (_contents == nil) {
        _contents = [NSMutableArray array];
    }
    
    return _contents;
}

-(NSMutableArray*)searchResult{
    if (_searchResult == nil) {
        _searchResult = [NSMutableArray array];
    }
    
    return _searchResult;
}


- (void)viewDidLoad {
self.title = @"通讯录";
   UIImageView *backImageView=[[UIImageView alloc]initWithFrame:self.view.bounds];
       [backImageView setImage:[UIImage imageNamed:@"通讯录背景"]];
self.tableView.backgroundView = backImageView;
    [super viewDidLoad];
    [self requestData];
    [self setRightItem];
}

-(void)requestData{
    [MBProgressHUD showHUDAddedTo:self.navigationController.view animated:YES];
    NSString * userId = [[NSUserDefaults standardUserDefaults]objectForKey:@"userId"];
    NSArray *  array = @[@{@"userId":userId}];;
    [HttpTools requestWithURL:KExchother arrays:array method:@"globalTxlTree" success:^(id JSON) {
        [MBProgressHUD hideAllHUDsForView:self.navigationController.view animated:YES];
        NSLog(@"%@",JSON);
        NSArray * tempArray = JSON[@"data"];
        NSDictionary *dict = [tempArray firstObject];
        self.contents = dict[@"Parent"];
        [self.tableView reloadData];
    } failure:^(NSError *error) {
        [MBProgressHUD hideAllHUDsForView:self.navigationController.view animated:YES];
    }];
}

-(void)setRightItem{
    UIBarButtonItem * rightitem = [[UIBarButtonItem alloc] initWithTitle:@"搜索" style:UIBarButtonItemStylePlain target:self action:@selector(searchAction)];
    self.navigationItem.rightBarButtonItem = rightitem;
}

-(void)searchAction{

    UIAlertView * alertView = [[UIAlertView alloc] initWithTitle:@"搜索通讯录" message:nil delegate:self cancelButtonTitle:@"取消" otherButtonTitles:@"确定", nil];
    alertView.delegate = self;
    alertView.alertViewStyle = UIAlertViewStylePlainTextInput;
    UITextField * textField = [alertView textFieldAtIndex:0];
    textField.placeholder = @"请输入关键字";
    [alertView show];
}

- (void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex{

    if (buttonIndex == 1) {
        UITextField * textField = [alertView textFieldAtIndex:0];
        if (textField.text.length == 0) {
            return;
        }
       
    OADepAddressController *per = [[OADepAddressController alloc]init];
    per.isNeedRefresh = YES;
    per.string = textField.text;
    
[self.navigationController pushViewController:per animated:YES];
    
    
}
}
-(UITableViewCell*)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    AddressCellList * cell = [tableView dequeueReusableCellWithIdentifier:@"addressCellList" forIndexPath:indexPath];
    cell.backgroundColor = [UIColor clearColor];
    NSDictionary * dict = self.contents[indexPath.row];
    cell.subject.text = dict[@"name"];
    return cell;
}
-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    return self.contents.count;
}

-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
     NSDictionary * dict = self.contents[indexPath.row];
if ([dict[@"isthree"]isEqualToString:@""]) {
 UIStoryboard * storyBoard = [UIStoryboard storyboardWithName:@"Main" bundle:nil];
    OADepAddressController * depVC = (OADepAddressController*)[storyBoard instantiateViewControllerWithIdentifier:@"DepAddressID" ];
    depVC.path = dict[@"unid"];
    [self.navigationController pushViewController:depVC animated:YES];


}
    else{
       PersonViewController *per = [[PersonViewController alloc]init];
     NSDictionary *dict = self.contents[indexPath.row];
per.unid = dict[@"unid"];
[self.navigationController pushViewController:per animated:YES];
    
    }


    
}

@end
